package na.learn.java.threads;

public class ThreadSoccer implements Runnable {

	private String RESOURCE = "";

	public synchronized void useResource() {

		try {
			//while (RESOURCE.length() != 0) {
			if(System.currentTimeMillis() %2 ==0 )
				wait();
				System.out.println("Was waiting for thread " + RESOURCE);

			//}

			RESOURCE = Thread.currentThread().getName();
			
			System.out.println("Thread " + Thread.currentThread().getName() + " Using resource ");
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		//RESOURCE = "";
		//notifyAll();

	}

	public void initThreads() {
		ThreadSoccer socc = new ThreadSoccer();
		
		for (int count = 0; count < 5; count++) {
			Thread newThread = new Thread(socc);
			newThread.setName("" + count);
			newThread.start();
		}

	}

	public void run() {
		for (int count = 0; count < 3; count++) {
			//synchronized (RESOURCE) {
				useResource();
			//}
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ThreadSoccer soccer = new ThreadSoccer();
		soccer.initThreads();

	}

}
